package com.sgb.kjwl.view.widget;

import android.content.Context;

import androidx.recyclerview.widget.GridLayoutManager;

import android.view.View;

import com.sgb.kjwl.R;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.packers.PickResultEntity;
import com.sgb.kjwl.view.ui.activity.lookingforAJob.FindWorksActivity;
import com.sgb.link.view.adapter.PickAdapter;
import com.swgk.core.customview.MaxRecyclerView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.List;

/**
 * 筛选弹窗（劳务分包）
 */
public class FindWorksFilterPopWindow extends BasePopWindow {

    private FindWorksActivity activity;
    private FilterResultClick filterResultClick;

    //工程分类/工程专项/专业分类/专业分类 adapter
    private PickAdapter projectAdapter, majorAdapter, businessAdapter, areaAdapter;

    private PickResultEntity pickResultProject, pickResultMajor, pickResultBusiness, pickResultArea;

    public FindWorksFilterPopWindow(Context context) {
        super(context, DIR_RIGHT);
        activity = (FindWorksActivity) context;
    }

    @Override
    protected int popLayout() {
        return R.layout.popwin_find_works_fliter;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        //工程分类rv
        MaxRecyclerView rvProject = holder.getPop().findViewById(R.id.rv_project);
        //工程专项rv
        MaxRecyclerView rvMajor = holder.getPop().findViewById(R.id.rv_major);
        //专业分类rv
        MaxRecyclerView rvBusiness = holder.getPop().findViewById(R.id.rv_business);
        //所在区域rv
        MaxRecyclerView rvArea = holder.getPop().findViewById(R.id.rv_area);

        if (pickResultProject == null) {
            pickResultProject = new PickResultEntity();
        }
        if (pickResultMajor == null) {
            pickResultMajor = new PickResultEntity();
        }
        if (pickResultBusiness == null) {
            pickResultBusiness = new PickResultEntity();
        }
        if (pickResultArea == null) {
            pickResultArea = new PickResultEntity();
        }

        holder.getPop().findViewById(R.id.ok_view).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                filterResultClick.result(pickResultProject, pickResultMajor, pickResultBusiness, pickResultArea);
                dissMiss();
            }
        });

        holder.getPop().findViewById(R.id.reset_view).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                projectAdapter.resetCheckDatas();
                majorAdapter.resetCheckDatas();
                businessAdapter.resetCheckDatas();
                areaAdapter.resetCheckDatas();

                projectAdapter.setDefItem(-1);
                majorAdapter.setDefItem(-1);
                businessAdapter.setDefItem(-1);
                areaAdapter.setDefItem(-1);

                pickResultProject.clear();
                pickResultMajor.clear();
                pickResultBusiness.clear();
                pickResultArea.clear();
            }
        });

        projectAdapter = new PickAdapter(mContext, null, R.layout.item_pick_filter_child);
        projectAdapter.setItemSelect(true);
        projectAdapter.setDefItem(-1);
        rvProject.setLayoutManager(new GridLayoutManager(mContext, 3));
        rvProject.setAdapter(projectAdapter);
        projectAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                pickResultProject.setNo(pickSelectEntity.getNo());
                pickResultProject.setName(pickSelectEntity.getProjectscale());
                pickResultProject.setPosition(position);
                pickResultMajor.clear();
                pickResultBusiness.clear();
                businessAdapter.clearDatas();
                //activity.viewModel.getSecondCategory(pickSelectEntity.getNo(), true);
            }
        });

        majorAdapter = new PickAdapter(mContext, null, R.layout.item_pick_filter_child);
        majorAdapter.setItemSelect(true);
        majorAdapter.setDefItem(-1);
        rvMajor.setLayoutManager(new GridLayoutManager(mContext, 3));
        rvMajor.setAdapter(majorAdapter);
        majorAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                pickResultMajor.setNo(pickSelectEntity.getNo());
                pickResultMajor.setName(pickSelectEntity.getProjectscale());
                pickResultMajor.setPosition(position);
                pickResultBusiness.clear();
                //activity.viewModel.getThrCategory(pickSelectEntity.getNo(), true);
            }
        });

        businessAdapter = new PickAdapter(mContext, null, R.layout.item_pick_filter_child);
        businessAdapter.setItemSelect(true);
        businessAdapter.setDefItem(-1);
        rvBusiness.setLayoutManager(new GridLayoutManager(mContext, 3));
        rvBusiness.setAdapter(businessAdapter);
        businessAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                pickResultBusiness.setNo(pickSelectEntity.getNo());
                pickResultBusiness.setName(pickSelectEntity.getProjectscale());
                pickResultBusiness.setPosition(position);
            }
        });

        areaAdapter = new PickAdapter(mContext, null, R.layout.item_pick_filter_child);
        areaAdapter.setItemSelect(true);
        areaAdapter.setDefItem(-1);
        rvArea.setLayoutManager(new GridLayoutManager(mContext, 3));
        rvArea.setAdapter(areaAdapter);
        areaAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                pickResultArea.setNo(pickSelectEntity.getNo());
                pickResultArea.setName(pickSelectEntity.getProjectscale());
                pickResultArea.setPosition(position);
            }
        });
    }

    public void setProjectData(List<PickSelectEntity> data, PickResultEntity entity) {
        projectAdapter.setDatas(data);
        projectAdapter.setDefItem(entity.getPosition());
        pickResultProject.replace(entity);
    }

    public void setMajorData(List<PickSelectEntity> data, PickResultEntity entity, boolean isRight) {
        majorAdapter.setDatas(data);
        if (!isRight) {
            pickResultMajor.replace(entity);
            majorAdapter.setDefItem(entity.getPosition());
        } else {
            majorAdapter.setDefItem(-1);
        }
    }

    public void setBusinessData(List<PickSelectEntity> data, PickResultEntity entity, boolean isRight) {
        businessAdapter.setDatas(data);
        if (!isRight) {
            pickResultBusiness.replace(entity);
            businessAdapter.setDefItem(entity.getPosition());
        } else {
            businessAdapter.setDefItem(-1);
        }
    }

    public void setAreaData(List<PickSelectEntity> data, PickResultEntity entity) {
        areaAdapter.setDatas(data);
        areaAdapter.setDefItem(entity.getPosition());
        pickResultArea.replace(entity);
    }

    public interface FilterResultClick {

        void result(PickResultEntity pickResultProject, PickResultEntity pickResultMajor, PickResultEntity pickResultBusiness, PickResultEntity pickResultArea);
    }

    public void setFilterResultClick(FilterResultClick filterResultClick) {
        this.filterResultClick = filterResultClick;
    }
}
